package utilitarios;

import dao.GenericDAO;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author WELISSON
 */
public class ExecutaQuery {
    
    public static void main(String args[]) throws SQLException {
        Connection conn = new GenericDAO().getConn();
        ExecutaQuery eq = new ExecutaQuery();
        conn.setAutoCommit(false);
        try {
            System.out.println("RUN");
            PreparedStatement ps = conn.prepareStatement(eq.readAsString());
            ps.execute();
            
            
            conn.commit();
        } catch (SQLException ex) {
            ex.printStackTrace();
            conn.rollback();
        }
        conn.setAutoCommit(true);
        System.out.println("FINISHED");
    }

    public String readAsString() {
        try {
            BufferedReader in = new BufferedReader(new FileReader("src/testes/insert_logradouro.sql"));
            String line;
            StringBuffer strBuffer = new StringBuffer();
            while ((line = in.readLine()) != null) {
                strBuffer.append(line);
                strBuffer.append('\n');
            }

            String contentStr = strBuffer.toString();
            return contentStr;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
